<template>
  <div id="applyInvoiceList">
    <Filters
      block
      :form="searchForm"
      :filterData="filterData"
      @confirm="changePage(1)"
    />
    <div class="container">
      <DataTable
        rowKey="phid"
        :btns="btns"
        :tableData="Data"
        :tableColumn="tableColumn"
        :pageConfig="pageConfig"
        @refresh="getList"
      />
    </div>
    <a-modal
      title="选择项目"
      :visible="formVisible"
      @cancel="hideFormVisible"
      :footer="null"
      width="70%">
      <div>
        <div>
          <div>
            <div style="color: rgba(0, 0, 0, 0.45);padding-left:10px">项目：</div>
            <a-input-search placeholder="请输入项目名称 " style="width: 200px" v-model="searchProject.name" @search="searchWl"/>
          </div>
          <DataTable rowKey="phidProject"  :showConfig="false" :tableData="Data2" :tableColumn="tableColumn2" :pageConfig="pageConfig2"></DataTable>
        </div>
      </div>
    </a-modal>
<!--    <CommonDialog :rows="rows" :form="form" title="作废原因" :visible="formVisible" @hideDialog="hideFormVisible" @confirm="confirmHandle()"/>-->
  </div>
</template>
<script lang='ts'>
import { Confirm } from '@/decorator';
import { Component, Watch, Vue } from 'vue-property-decorator';

@Component
export default class applyInvoiceList extends Vue {
  formVisible: any = false
  orglist: any[] = []
  projectid: any = ''
  searchForm: any = {
    projectName: '',
    billdate: [],
    markTime: [],
    billcode: '',
    billstatus: '',
    enablestate: '',
    name: ''
  }
  searchProject:any={
    name: ''
  }
  btns: any[] = [
    {
      label: '新增放行单',
      icon: 'add',
      callback: () => this.$router.push('/wasteManage/applyInvoiceEdit')
    }
  ];
  get filterData () {
    return {
      projectName: {
        label: '项目',
        type: 'input',
        width: 200,
        template: {
          placeholder: '项目',
          clearable: true,
          filterable: true,
          oc: this.showDiag
        }
      },
      billdate: {
        label: '单据日期',
        type: 'daterange',
        template: {
          clearable: true
        }
      },
      billcode: {
        label: '单据编号',
        type: 'input',
        template: {
          placeholder: '请输入验收编号',
          clearable: true
        }
      },
      billstatus: {
        label: '单据状态',
        type: 'select',
        width: 200,
        template: {
          placeholder: '请选择状态',
          clearable: true,
          filterable: true,
          options: this.$enums.acceptanceList.billOptions
        }
      }
    }
  }
  Data: any[] = [];
  tableColumn: any = {
    billcode: {
      label: '单据编号'
    },
    billdate: {
      label: '单据日期'
    },
    billStatusName: {
      label: '单据状态',
      width: 100
    },
    complanyName: {
      label: '分公司'
    },
    orgName: {
      label: '组织'
    },
    projectName: {
      label: '项目'
    },
    registerName: {
      label: '申请人'
    },
    action: {
      label: '操作',
      width: 150,
      template: [
        {
          tag: 'router-link',
          text: '详情',
          query: {
            _alias: 'detail',
            phid: ''
          },
          // show: ({checkAcceptStatus}: any) => ['dsh', 'ok', 'nc', 'no', 'dfy', 'dtj', 'yszf'].includes(checkAcceptStatus),
          path: '/wasteManage/passDetail'
        },
        {
          tag: 'router-link',
          text: '编辑',
          query: {
            _alias: 'edit',
            phid: ''
          },
          show: ({billStatusName}: any) => ['保存态'].includes(billStatusName),
          path: '/wasteManage/applyInvoiceEdit'
        },
        {
          text: '审核',
          tag: 'router-link',
          query: {
            _alias: 'aduit',
            phid: ''
          },
          // show: ({checkAcceptStatus, hasAudit}: any) => ['dsh'].includes(checkAcceptStatus) && hasAudit === 'Y',
          path: '/wasteManage/applyInvoiceDetail'
        }

      ]
    }
  };
  Data2: any[] = [];
  tableColumn2: any = {
    ncProjectId: {
      label: '项目编码',
      minWidth: 120
    },
    ncProjectName: {
      label: '项目名称',
      minWidth: 120,
      hidden: true
    },
    action: {
      label: '操作',
      width: 120,
      template: [
        {
          tag: 'a',
          text: '确定',
          callback: this.confirmProject
        }
      ]

    }
  };
  pageConfig: any = {
    current: 1,
    total: 10,
    pageSize: 10,
    onChange: this.changePage
  };
  pageConfig2: any = {
    current: 1,
    total: 10,
    pageSize: 10,
    onChange: this.changePage2
  };
  form: any = {
    cancelReason: '',
    phid: ''
  };
  get rows () {
    return {
      cancelReason: {
        label: '作废原因',
        placeholder: '请输入作废原因',
        type: 'textarea',
        rules: [{ required: true, message: '请输入作废原因', trigger: 'blur' }]
      }

    }
  }

  get getSearchForm () {
    let {billdate, markTime} = this.searchForm
    let [makeStartTime, makeEndTime] = markTime
    let [checkStartTime, checkEndTime] = billdate
    let phidProject = this.projectid
    return {
      ...this.searchForm,
      markTime: undefined,
      billdate: undefined,
      makeStartTime,
      makeEndTime,
      checkStartTime,
      checkEndTime,
      phidProject
    }
  }
  showDiag () {
    this.getProjectList()
    this.formVisible = true
  }
  // 搜索物料
  searchWl (val:any) {
    this.searchProject.name = val
    this.getProjectList()
  }
  confirmProject (val: any, cb:any) {
    this.projectid = val.ncProjectId
    this.searchForm['projectName'] = val.ncProjectName
    this.formVisible = false
  }
  // 项目列表
  getProjectList () {
    let word = this.searchProject.name
    let org = this.orgid
    let { current, pageSize } = this.pageConfig2
    let data:any = {}
    data.page = current
    data.size = pageSize
    if (org) {
      data.id = org
    }
    if (word) {
      data.searchValue = word
    }
    this.$api.docInfoProjectListFetch(data).then((res: any) => {
      let projectList = res.data.records.map((item:any) => { item.label = item.ncProjectName; item.value = item.ncProjectId })
      // alert(JSON.stringify(orgList));
      this.projectList = res.data.records
      this.Data2 = res.data.records

      console.log(res.data)
      this.pageConfig2.total = res.data.total;
    }).catch()
  }

  init () {
    // this.getProvince()
    this.getList();
  }
  getList () {
    let data = {
      ...this.getSearchForm,
      page: this.pageConfig.current,
      size: this.pageConfig.pageSize
    };
    this.$api.passListFetch(data).then((res: any) => {
      res.data.records.map((item:any) => {
        // eslint-disable-next-line eqeqeq
        if (item.isContract == 1) {
          item.isContract = true
        } else {
          item.isContract = false
        }
      })
      console.log(res.data.records);
      this.Data = res.data.records;
      this.pageConfig.total = res.data.total;
      if (!this.Data.length && this.pageConfig.current > 1) {
        --this.pageConfig.current;
        this.getList();
      }
    });
  }
  hideFormVisible () {
    this.formVisible = false
    this.form.cancelReason = ''
    this.form.phid = ''
  }
  changePage (val: number) {
    this.pageConfig.current = val;
    this.getList();
  }
  changePage2 (val: number) {
    this.pageConfig2.current = val;
    this.getProjectList();
  }
  @Confirm('', ({ checkSn }: any) => {
    return new Promise((resolve, reject) => {
      resolve({ content: `确定撤回[${checkSn}]验收单吗?`, width: 550 })
    })
  })
  handleCancel ({ phid: id }: any) {
    this.$api.checkWithdrawPortFetch({ id, checkAcceptStatus: 'chehui' }).then(() => {
      this.$message.success('撤回成功')
      this.getList()
    })
  }
  @Confirm('', ({ checkSn }: any) => {
    return new Promise((resolve, reject) => {
      resolve({ content: `确定把[${checkSn}]的验收单推送NC吗?`, width: 550 })
    })
  })
  handlePush ({ phid: id }: any) {
    this.$api.checkWithdrawPortFetch({ id, checkAcceptStatus: 'nc' }).then(() => {
      this.$message.success('推送成功')
      this.getList()
    })
  }
  // 显示输入框
  showFormVisible (val:any) {
    console.log(val)
    this.formVisible = true
    this.form.phid = val.phid
  }
  // 点击确定作废
  confirmHandle () {
    this.$api.cancelCheckCancelCheckFetch(this.form).then(res => {
      // eslint-disable-next-line eqeqeq
      if (res.code == 0) {
        this.$message.success('操作成功')
        this.getList()
        this.hideFormVisible()
      }
    }).catch()
  }
}
</script>
<style lang='scss' scoped>
</style>
